/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

public class PreferenceHelper {
	public static final int RANK_TYPE_BY_TIME = 0;
	public static final int RANK_TYPE_BY_COUNT = 1;
	public static final int RANK_TYPE_BY_LAST = 2;

	public static final String KEY_START_STAT = "START_STAT";
	public static final String KEY_LAST_STAT = "LAST_STAT";
	public static final String KEY_RANK_TYPE = "RANK_TYPE";
	//the two key below are the same value as in strings.xml
	public static final String KEY_LAUNCH_CONFIRM = "LAUNCH_CONFIRM";
	public static final String KEY_SERVICE_RUNNING = "SERVICE_RUNNING";

	private static final long DEFAULT_STAT_START_TIME = -1;
	private static final long DEFAULT_LAST_STAT_TIME = -1;
	private static final int DEFAULT_RANK_TYPE = RANK_TYPE_BY_LAST;
	private static final boolean DEFAULT_LAUNCH_CONFIRM = true;
	private static final boolean DEFAULT_SERVICE_RUNNING = true;

	/**
	 * 
	 * @return the milliseconds of the start statistic time
	 */
	public static long getStatisticStartTime(Context context) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		return preference.getLong(KEY_START_STAT, DEFAULT_STAT_START_TIME);
	}

	/**
	 * check if the Statistic Start Time has been set
	 * 
	 * @param context
	 * @return true if set , otherwise false
	 */
	public static boolean isStatisticStartTimeSet(Context context) {
		long start = getStatisticStartTime(context);
		if (start == DEFAULT_STAT_START_TIME) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * set the Statistic Start Time into the preference file.
	 * 
	 * @param context
	 * @param val
	 *            the value to be set
	 */
	public static void setStatisitcStartTime(Context context, long val) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = preference.edit();
		editor.putLong(KEY_START_STAT, val);
		editor.commit();
	}

	/**
	 * @return the milliseconds of the last statistic time
	 */
	public static long getLastStatisticTime(Context context) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		return preference.getLong(KEY_LAST_STAT, DEFAULT_LAST_STAT_TIME);
	}

	/**
	 * set the Last Statistic Time into the preference file.
	 * 
	 * @param context
	 * @param val
	 *            the value to be set
	 */
	public static void setLastStatisitcTime(Context context, long val) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = preference.edit();
		editor.putLong(KEY_LAST_STAT, val);
		editor.commit();
	}

	/**
	 * @return the rank type , either RANK_TYPE_BY_TIME or RANK_TYPE_BY_COUNT
	 */
	public static int getRankType(Context context) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		return preference.getInt(KEY_RANK_TYPE, DEFAULT_RANK_TYPE);
	}

	/**
	 * set the rank type into the preference file.
	 * 
	 * @param context
	 * @param val
	 *            the value to be set
	 */
	public static void setRankType(Context context, int val) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = preference.edit();
		editor.putInt(KEY_RANK_TYPE, val);
		editor.commit();
	}

	/**
	 * @return the launch confirm , true to show launch confirm , otherwise
	 *         false.
	 */
	public static boolean getLaunchConfirm(Context context) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		return preference
				.getBoolean(KEY_LAUNCH_CONFIRM, DEFAULT_LAUNCH_CONFIRM);
	}

	/**
	 * set the the launch confirm into the preference file.
	 * 
	 * @param context
	 * @param val
	 *            the value to be set
	 */
	public static void setLaunchComfirm(Context context, boolean val) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = preference.edit();
		editor.putBoolean(KEY_LAUNCH_CONFIRM, val);
		editor.commit();
	}

	/**
	 * @return the service switch, true to start service , false to stop service.
	 */
	public static boolean isServiceRunning(Context context) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		return preference
				.getBoolean(KEY_SERVICE_RUNNING, DEFAULT_SERVICE_RUNNING);
	}

	/**
	 * set the service switch into the preference file.
	 * 
	 * @param context
	 * @param val
	 *            the value to be set
	 */
	public static void setServiceRunning(Context context, boolean val) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = preference.edit();
		editor.putBoolean(KEY_SERVICE_RUNNING, val);
		editor.commit();
	}

	
	/**
	 * Registers a callback to be invoked when a change happens to a preference
	 * in the default shared preference.
	 * 
	 * @param context
	 *            The context of the preferences whose values are wanted.
	 * @param listener
	 *            The callback that will run.
	 */
	public static void registerOnSharedPreferenceChangeListener(Context context,
			OnSharedPreferenceChangeListener listener) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		preference.registerOnSharedPreferenceChangeListener(listener);
	}

	/**
	 * Unregisters a previous callback to the default shared preference.
	 * 
	 * @param listener
	 *            The callback that should be unregistered.
	 * @see #registerOnSharedPreferenceChangeListener
	 */
	public static void unregisterOnSharedPreferenceChangeListener(Context context,
			OnSharedPreferenceChangeListener listener) {
		SharedPreferences preference = PreferenceManager
				.getDefaultSharedPreferences(context);
		preference.unregisterOnSharedPreferenceChangeListener(listener);
	}
}
